package com.dream.es.offical;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.TotalHits;
import co.elastic.clients.elasticsearch.core.search.TotalHitsRelation;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class Example {

    public static void main(String[] args) throws IOException {

        ElasticsearchClient esClient = createElasticsearchClient();

        search(esClient);

    }

    private static void search(ElasticsearchClient esClient) {
        try {
            SearchResponse<DataItemIns> resp = esClient.search(s -> s
                            .index("ins_task")
                            .query(q -> q
                                    .match(t -> t
                                            .field("fields.system_name.value")
                                            .query("王五")
                                    )
                            )
                    ,
                    DataItemIns.class
            );

            TotalHits total = resp.hits().total();
            boolean isExactResult = total.relation() == TotalHitsRelation.Eq;

            if (isExactResult) {
                System.out.println("There are " + total.value() + " results");
            } else {
                System.out.println("There are more than " + total.value() + " results");
            }

            List<Hit<DataItemIns>> hits = resp.hits().hits();
            for (Hit<DataItemIns> hit: hits) {
                DataItemIns ins = hit.source();
                System.out.println("Found data item ins, dataItemId: " + ins.getDataItemId() + ", fields:  " + ins.getFields());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static ElasticsearchClient createElasticsearchClient() {
        RestClient client = createClient();

        RestClientTransport transport = new RestClientTransport(client, new JacksonJsonpMapper());

        return new ElasticsearchClient(transport);
    }

    private static RestClient createClient() {
        return RestClient
                .builder(HttpHost.create("http://elastic-elasticsearch-master.elastic:9200"))
//                .setDefaultHeaders(new Header[]{
//                        new BasicHeader("Authorization", "ApiKey " + "")
//                })
                .build();
    }


    private static class DataItemIns {
        private String id;
        private String dataItemId;
        private String dataItemType;
        private Map<String, Object> fields;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getDataItemId() {
            return dataItemId;
        }

        public void setDataItemId(String dataItemId) {
            this.dataItemId = dataItemId;
        }

        public String getDataItemType() {
            return dataItemType;
        }

        public void setDataItemType(String dataItemType) {
            this.dataItemType = dataItemType;
        }

        public Map<String, Object> getFields() {
            return fields;
        }

        public void setFields(Map<String, Object> fields) {
            this.fields = fields;
        }
    }
}
